package com.zhangst.bbs.action;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zhangst.bbs.domain.Role;
import com.zhangst.bbs.domain.User;
import com.zhangst.bbs.page.Json;
import com.zhangst.bbs.page.UserModel;
import com.zhangst.bbs.service.UserServiceI;
import com.zhangst.bbs.util.GenericUtil;
import com.zhangst.bbs.util.StringUtil;

@ParentPackage("basePackage")
@Namespace("/")
public class UserAction extends BaseAction implements ModelDriven<UserModel> {

	private UserServiceI userService;
	private UserModel userModel = new UserModel();

	public UserServiceI getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	@Action(value = "register")
	public void register() {
		String username = userModel.getUsername();
		String psw = userModel.getPsw();
		String psw2 = userModel.getPsw2();
		Json json = new Json();
		if(StringUtil.isBlank(username)||StringUtil.isBlank(psw)||StringUtil.isBlank(psw2)){
			json.setMsg("用户名或密码不能为空！");
			json.setSuccess(false);
		}else if(!psw.equals(psw2)){
			json.setMsg("两次密码不一致！");
			json.setSuccess(false);
		}else if(userService.getUser(username)!=null){
			json.setMsg("用户名已存在！");
			json.setSuccess(false);
		}
		else{
			Role role = new Role();
			role.setId(1);
			User user = new User();
			user.setUsername(username);
			user.setPsw(GenericUtil.MD5(psw));
			user.setRole(role);
			userService.add(user);
			json.setMsg("注册成功！");
			json.setSuccess(true);
		}
		writeJson(json);
	}
	
	
	
	@Action(value="login")
	public void login(){
		String username=userModel.getUsername();
		String psw = userModel.getPsw();
		Json json = new Json();
		if(StringUtil.isBlank(username)||StringUtil.isBlank(psw)){
			json.setMsg("用户名或密码不能为空！");
			json.setSuccess(false);
		}else {
			User user = new User();
			user.setUsername(username);
			user.setPsw(psw);
			user = userService.login(user);
			if(user ==null){
				json.setMsg("用户名或密码错误！");
				json.setSuccess(false);
			}else{
				ActionContext actionContext = ActionContext.getContext();
				Map session = actionContext.getSession();
				session.put("user", user);
				json.setMsg("登录成功！");
				json.setSuccess(true);
			}
			
		}
		
		writeJson(json);
	}
	
	@Action(value="logout")
	public void logout(){
		Json json = new Json();
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.remove("user");
		json.setMsg("注销成功！");
		json.setSuccess(true);
		writeJson(json);
	}
	
	
	@Action(value="change")
	public void change(){
		String psw = userModel.getPsw();
		String psw1 = userModel.getPsw1();
		String psw2 = userModel.getPsw2();
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		User user = (User)session.get("user");
		Json json  =  new Json();
		if(user== null){
			json.setMsg("请先登录！");
			json.setSuccess(false);
		}else if(StringUtil.isBlank(psw)||StringUtil.isBlank(psw1)||StringUtil.isBlank(psw2)){
			json.setMsg("密码不能为空！");
			json.setSuccess(false);
		}else if(!psw1.equals(psw2)){
			json.setMsg("新密码不一致！");
			json.setSuccess(false);
		}else if(psw.equals(psw1)){
			json.setMsg("新旧密码不能一致！");
			json.setSuccess(false);
		}else if(!GenericUtil.MD5(psw).equals(user.getPsw())){
			json.setMsg("旧密码错误！");
			json.setSuccess(false);
		}else{
			user.setPsw(GenericUtil.MD5(psw1));
			userService.edit(user);
			session.remove("user");
			json.setMsg("修改成功！");
			json.setSuccess(true);
		}
		writeJson(json);
	}
	
	
	@Override
	public UserModel getModel() {
		return userModel;
	}
}
